package com.occludens.admin;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;

@Entity
@Cache
public abstract class AdminObject {
	
	public static Key<AdminObject> key(long id) {
		return Key.create(AdminObject.class, id);
	}
	
	@Id Long id;
	@Index private String name = "";
	@Index private String description = "";

	protected AdminObject() {} // Mandatory for Objectify

	public AdminObject (String name) {
		this.name = name;
	}
	
	public void save() {
		ObjectifyService.ofy().save().entity(this).now();
	}
	
	protected Key<?> getKey() {
		return Key.create(AdminObject.class, id);
	}

	public Long getId() {
		return this.id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public boolean checkUniqueness(String name, Class className) {
		if (ObjectifyService.ofy().load().type(className).filter("name", name).list().size()>0) {
			return false;
		} else {
			return true;
		}
	}

}
